package errcode

import (
	"fmt"
)

// 定义业务组常量
const (
	BusinessGroupUser = iota + 1
	BusinessGroupOrder
	BusinessGroupPayment
	BusinessGroupAssets
	// 添加更多业务组
)

// 定义业务层常量
const (
	LayerAPI = 0
	LayerRPC = 1
)

const (
	_ = iota
	ErrDuplicatedKey
	ErrNoRows
	ErrInsert
	ErrDataConvert
	ErrInvalidAmount
	ErrNoRowsAffected
	ErrUpdate
	ErrInternalErr
)

// 使用 iota 定义错误码常量
const (
	_                           = iota // 跳过 0
	AssetsRPCErrDuplicatedKey   = BusinessGroupAssets*(1000000000) + LayerRPC*100000 + ErrDuplicatedKey
	AssetsRPCErrNoRows          = BusinessGroupAssets*(1000000000) + LayerRPC*100000 + ErrNoRows
	AssetsRPCErrInsert          = BusinessGroupAssets*(1000000000) + LayerRPC*100000 + ErrInsert
	AssertsRPCErrDataConvert    = BusinessGroupAssets*(1000000000) + LayerRPC*100000 + ErrDataConvert
	AssertsRPCErrInvalidAmount  = BusinessGroupAssets*(1000000000) + LayerRPC*100000 + ErrInvalidAmount
	AssertsRPCErrNoRowsAffected = BusinessGroupAssets*(1000000000) + LayerRPC*100000 + ErrNoRowsAffected
	AssertsRPCErrUpdate         = BusinessGroupAssets*(1000000000) + LayerRPC*100000 + ErrUpdate
	AssertsRPCErrInternalErr    = BusinessGroupAssets*(1000000000) + LayerRPC*100000 + ErrInternalErr
)

// 定义语言常量
const (
	LangZhCN = "zh-CN" // 简体中文
	LangEnUS = "en-US" // 英语
	// 添加更多语言
)

// 错误码结构体
type Errcode struct {
	Code int64
	Msg  string
}

func (e *Errcode) Error() string {
	return e.Msg
}

// 错误消息存储
var Messages = map[int64]map[string]string{
	AssetsRPCErrDuplicatedKey: {
		LangZhCN: "订单号重复",
		LangEnUS: "Duplicated order number",
	},
	AssetsRPCErrNoRows: {
		LangZhCN: "记录不存在",
		LangEnUS: "Record not found",
	},
	AssetsRPCErrInsert: {
		LangZhCN: "保存数据失败",
		LangEnUS: "Failed saving data",
	},
	AssertsRPCErrDataConvert: {
		LangZhCN: "数据转换失败",
		LangEnUS: "Failed converting data",
	},
	AssertsRPCErrInvalidAmount: {
		LangZhCN: "无效的金额",
		LangEnUS: "Invalid amount",
	},
	AssertsRPCErrNoRowsAffected: {
		LangZhCN: "没有受影响的记录",
		LangEnUS: "No rows affected",
	},
	AssertsRPCErrUpdate: {
		LangZhCN: "修改失败",
		LangEnUS: "Update failed",
	},
}

// 生成错误码
func GenerateErrorCode(businessGroup, layer, code int64) int64 {
	return businessGroup*1000000000 + layer*100000 + code
}

// 创建新的错误码
func NewError(businessGroup, layer, code int64, langs ...string) *Errcode {
	c := GenerateErrorCode(businessGroup, layer, code)
	lang := LangEnUS // 默认语言设置为英语

	if len(langs) > 0 {
		lang = langs[0]
	}

	msg, ok := Messages[c][lang]
	if !ok {
		// 如果没有对应语言的消息，尝试使用默认语言
		if lang != LangEnUS {
			msg, ok = Messages[c][LangEnUS]
		}
		// 如果仍没有找到，使用错误码作为消息
		if !ok {
			msg = fmt.Sprintf("Error code: %d", c)
		}
	}
	return &Errcode{
		Code: c,
		Msg:  fmt.Sprintf("Error code: %d, Message: %s", c, msg),
	}
}

func NewAssetsRPCError(code int64, langs ...string) *Errcode {
	return NewError(BusinessGroupAssets, LayerRPC, code, langs...)
}

func NewAssetsAPIError(code int64, langs ...string) *Errcode {
	return NewError(BusinessGroupAssets, LayerAPI, code, langs...)
}

func Wrap(err error) *Errcode {
	return &Errcode{
		Code: AssertsRPCErrInternalErr,
		Msg:  err.Error(),
	}
}
